#include "mycgi.h"
#include <string.h>

char *queryPath()
{
    char *uri = cgiRequestURI();

    if(!uri) return 0;

    char *scriptName = cgiScriptName();
    char *queryString = cgiQueryString();

    char *pathStart = uri;

    if(scriptName)
    {
        char *newStart = strstr(uri,scriptName);
        if(newStart) pathStart = newStart + strlen(scriptName);
    }

    char *pathEnd = pathStart + strlen(pathStart);

    if(queryString)
    {
        char *newEnd = strstr(pathStart,queryString);
        if(newEnd) pathEnd = newEnd - 1;
    }

    char *queryPath = strndup(pathStart,pathEnd-pathStart);

    return queryPath;
}

int showTestView(CGI *cgi)
{
    #include "views/testView.c"
}

int appBegin()
{
    CGI *cgi = cgiInit();
    if(!cgi) return -1;

    cgiLoadVars(cgi);
/*
    char *qPath = queryPath();
    if(!qPath) return -1;

    showTestView(cgi);

    cgiStartFlush(cgi);

    free(qPath);
*/

    cgiDelete(cgi);

    return 0;
}

int main(void)
{
    return appBegin();
}
